/**
 * Created by wry on 2014/12/29.
 */
var adminService = require('../../services/backoffice/backOffice-restService');
var toolUtil = require('../../common/utils/toolUtil');
var messageUtil = require('../../common/utils/messageUtil');

exports.createRESTService = function (req, res) {
    var input = req.body;
    adminService.createRESTService(input, function (err, result) {
        if (err) {
            logger.error("Failed to create RESTFull service - " + err.message);
            res.json(messageUtil.composeFailedMessage(req,"Failed to post question - " + err.message));
        } else {
            logger.debug("Succeeded to create RESTFull service! - " + result._id);
            res.json(messageUtil.composeSucceededMessage(req,null, result._id));
        }
    });
}

exports.updateRESTService = function (req, res) {

    var id = req.params.RESTServiceObjId;
    var input = req.body;

    adminService.updateRESTService(id, input, function (err, results) {
        if (err) {
            logger.error('Exception happened when trying to update the RESTFull Service data...!'+err.message);
            res.json(messageUtil.composeFailedMessage(req,'Exception happened when trying to update the RESTFull Service  data...!'));
        } else {
            if (results != 0) {
                logger.debug("Update RESTFull Service  successfully...!");
                res.json(messageUtil.composeSucceededMessage(req,null, null));
            } else {
                logger.error('Failed to update the RESTFull Service , it is not existing! -' + id);
                res.json(messageUtil.composeFailedMessage(req,'Failed to update the RESTFull Service , it is not existing! -' + id));
            }
        }
    })
}

/**
 * 获取RESTFull的服务列表
 * @param req
 * @param res
 */
exports.getRESTServiceList = function (req, res) {
    var startPage = req.params.startPage;
    var pageSize  = req.params.pageSize;
    if(startPage==null)startPage=0;
    if(pageSize==null)pageSize=global.listPageSize;

    adminService.getRESTServiceList(startPage,pageSize,function (err, results) {
        if (err) {
            logger.error('Exception happened when get RESTFull List ...!' + token);
            res.json(messageUtil.composeFailedMessage(req,'Exception happened when RESTFull List...!'));
        } else {
            tempResult={
                'totalNum':results[0],
                'items':results[1]
            },
            res.json(messageUtil.composeSucceededMessage(req,null,tempResult));
        }
    });
}

exports.getRESTServiceById = function (req, res) {
    var id = req.params.RESTServiceObjId;
    adminService.getRESTServiceById(id, function (err, results) {
        if (err) {
            logger.error('Exception happened when get RESTFull by Id ...!' + token);
            res.json(messageUtil.composeFailedMessage(req,'Exception happened when get RESTFull by Id...!'));
        } else {
            res.json(messageUtil.composeSucceededMessage(req,null, results));
        }
    });

}

exports.deleteRESTServiceById = function (req, res) {

    var id = req.params.RESTServiceObjId;
    adminService.deleteRESTService(id, function (err, results) {
        if (err) {
            logger.error('Exception happened when trying to delete RESTFull Service data...!');
            res.json(messageUtil.composeFailedMessage(req,'Exception happened when trying to delete RESTFull Service  data...!'));
        } else {
            if (results != null) {
                logger.debug("Delete RESTFull Service  successfully...!");
                res.json(messageUtil.composeSucceededMessage(req,null, null));
            } else {
                logger.error('Failed to delete the RESTFull Service , it is not existing! -' + id);
                res.json(messageUtil.composeFailedMessage(req,'Failed to delete the RESTFull Service , it is not existing! -' + id));
            }
        }
    })
}